/**
 * 
 */
package com.sfeir.client.activity;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Image;
import com.google.web.bindery.event.shared.EventBus;
import com.sfeir.canvas.activities.client.AbstractCanvasActivity;
import com.sfeir.canvas.activities.client.event.PageChangeEvent;
import com.sfeir.canvas.util.client.ResourceManager;
import com.sfeir.client.i18n.TetrisConstants;
import com.sfeir.client.util.Constants;

/**
 * @author François LAROCHE
 * 
 */
public class CreditActivity extends AbstractCanvasActivity {

	private static final int BACK_BUTTON_X = 100;
	private static final int BACK_BUTTON_Y = 450;
	private static final int BACK_BUTTON_SIZE = 64;

	private final Image panel;
	private final Image back;
	private final Image background;
	private final Image logo;
	private final TetrisConstants texts;
	private EventBus bus;
	private Canvas canvas;

	public CreditActivity(ResourceManager manager) {
		this.panel = manager.getImage(Constants.KEY_IMAGE_PANEL);
		this.back = manager.getImage(Constants.KEY_IMAGE_BACK);
		this.logo = manager.getImage(Constants.KEY_IMAGE_LOGO_SFEIR);
		this.background = manager.getImage(Constants.KEY_IMAGE_BACKGROUND);
		this.texts = GWT.create(TetrisConstants.class);
	}


	@Override
	public void start(Canvas canvas, EventBus bus) {
		this.bus = bus;
		this.canvas = canvas;
		Context2d ctx = canvas.getContext2d();
		ctx.drawImage(ImageElement.as(background.getElement()), 0, 0);
		ctx.drawImage(ImageElement.as(panel.getElement()), 0, 0);
		ctx.drawImage(ImageElement.as(back.getElement()), BACK_BUTTON_X, BACK_BUTTON_Y);
		ctx.drawImage(ImageElement.as(logo.getElement()), 110, 80);
		ctx.save();
		ctx.setFont("13pt calibri");
		ctx.setFillStyle("black");
		ctx.fillText(texts.credits1(), 120, 250);
		ctx.fillText(texts.credits2(), 120, 300);
		ctx.fillText(texts.credits3(), 120, 350);
		ctx.fillText(texts.developers(), 200, 490);
		ctx.restore();
	}

	@Override
	public void onClick(ClickEvent event) {
		int x = event.getX();
		int y = event.getY();

		// Handle the click on the Back Button
		if (x >= BACK_BUTTON_X && x <= BACK_BUTTON_X + BACK_BUTTON_SIZE && y >= BACK_BUTTON_Y
				&& y <= BACK_BUTTON_Y + BACK_BUTTON_SIZE) {
			this.bus.fireEvent(new PageChangeEvent(PageChangeEvent.BACK, null));
		}

		// Handle the click on the Sfeir Link
		if (x >= 110 && x <= 310 && y >= 80 && y <= 166) {
			Window.open("http://www.sfeir.com", "sfeir", "");
		}
	}

	@Override
	public void onMouseMove(MouseMoveEvent event) {
		int x = event.getX();
		int y = event.getY();
		if (x >= 110 && x <= 310 && y >= 80 && y <= 166) {
			this.canvas.addStyleName("cursor");
		} else {
			this.canvas.removeStyleName("cursor");
		}
	}

}
